package com.mef.smsplat.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.cqcis.base.orm.jdbc.BaseJdbcDao;
import com.hc24.util.IDUtil;
import com.mef.smsplat.dao.base.BaseSpringJdbcDao;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.LimitInfoDomain;
import com.mef.smsplat.util.Constants;

@Repository
public class LimitInfoDao extends BaseSpringJdbcDao<LimitInfoDomain, String> {

	
	/**
	 * 根据权限名分页查询权限信息
	 * @param limitName
	 * @param dgm
	 * @return
	 */
	public Page query(String limitName,DataGridModel dgm){
		List<Object> params=new ArrayList<Object>();
		StringBuilder sql=new StringBuilder();
		sql.append("select * from limit_info where 1=1");
		if(limitName!=null&&!"".equals(limitName)){
			sql.append(" and limit_name like ?");
			params.add("%"+limitName+"%");
		}
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params);
	}

	@Override
	public BaseJdbcDao getDaoClass() {
		return this;
	}

	@Override
	public Class getEntityClass() {
		return LimitInfoDomain.class;
	}
	
	public List<LimitInfoDomain> queryParent(){
		String sql="select * from limit_info t where t.limit_father is null or t.limit_father=''";
		List<LimitInfoDomain> list=super.query(sql, new Object[]{});
		return list;
	}
	
	public List<LimitInfoDomain> querySubModule(String parentId){
		String sql="select * from limit_info t where t.limit_father=? order by t.limit_seq asc";
		List<LimitInfoDomain> list=super.query(sql, new Object[]{parentId});
		return list;
	}
	
	public LimitInfoDomain queryById(String limitId){
		String sql="select * from limit_info t where t.limit_id=?";
		List<LimitInfoDomain> list=super.query(sql, new Object[]{limitId});
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	
	/**
	 * 根据角色查询权限
	 * @param roleId
	 * @return
	 */
	public List<LimitInfoDomain> queryLimit(String roleId){
		return super.query("select * from limit_info l left join role_limit_info r on l.limit_id=r.limit_id where r.role_id=?", new Object[]{roleId});
	}
	
	/**
	 * 查询所有菜单
	 * @return
	 */
	public List<LimitInfoDomain> queryMenu(){
		return super.query("select * from limit_info l where l.limit_menu=?", new Object[]{Constants.IS_MENU});
	}

	/**
	 * 根据roleId删除
	 * @param roleId
	 */
	public void deleteLimitByRole(String roleId){
		super.getJdbcTemplate().execute("delete from role_limit_info where role_id="+roleId);
	}
	
	/**
	 * 设置权限
	 * @param roleId
	 * @param limitId
	 */
	public void addLimit(String roleId, String limitId){
		String sql = "insert into role_limit_info(role_limit_id,role_id,limit_id) values(?,?,?)";
		super.getJdbcTemplate().update(sql, new Object[]{IDUtil.next()+"",roleId,limitId});
	}
}
